package yf.mall.service.impl;

import com.yf.service.impl.BaseServiceImpl;
import yf.mall.enums.AccountRecordBusEnum;
import yf.mall.enums.AccountRecordStatusEnum;
import yf.mall.enums.AccountRecordTypeEnum;
import yf.mall.enums.AccountTypeEnum;
import yf.mall.vo.AccountRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import yf.mall.dao.AccountRecordDao;
import yf.mall.entity.AccountRecordEntity;
import yf.mall.service.AccountRecordService;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Mon Aug 26 16:58:11 CST 2019
 */
@Service
public class AccountRecordServiceImpl extends BaseServiceImpl<AccountRecordEntity> implements AccountRecordService {

    private AccountRecordDao accountRecordDao;


    @Autowired
    public void setDao(AccountRecordDao accountRecordDao) {
        this.accountRecordDao = accountRecordDao;
        super.setBaseDao(accountRecordDao);
    }



    /**内部方法**/


    /**外部方法实现**/


    @Override
    public List<AccountRecordVo> queryListVo(AccountRecordVo params) {
        return accountRecordDao.queryListVo(params);
    }

    @Override
    public AccountRecordVo getVo(Long id) {
        return accountRecordDao.getVo(id);
    }

    @Override
    public List<AccountRecordVo> findByUserId(Long userId,Integer type,Integer status,Integer bus) {
        List<AccountRecordVo> list = null;
        //收款页
        if (type == null){
            list  = accountRecordDao.findByFromUserId(userId, type, status,
                    AccountRecordBusEnum.PAYMENT.getIndex(),
                    AccountRecordBusEnum.SHARE_REWARD.getIndex(), AccountRecordBusEnum.AREA_REWARD.getIndex(),
                    AccountRecordBusEnum.SHOP.getIndex(), AccountRecordBusEnum.SHOP_MANAGER_REWARD.getIndex(),AccountRecordBusEnum.LEVEL_PROFIT.getIndex());
        }else {
            //金额明细页
           list =  accountRecordDao.findByUserId(userId,type,status,bus);
        }

        return list;
    }

    /*
       que
       店铺实际收款
     */
    @Override
    public Map<String,BigDecimal> findByToUserId(Long userId, Integer type, Integer bus, Integer status) {
        BigDecimal totalIncome = BigDecimal.ZERO;
        Map<String,BigDecimal> map = new HashMap<>();
        //已完成的收款
        List<AccountRecordEntity> recordEntityList = accountRecordDao.findByToUserId(userId, type, bus, status);
        if (recordEntityList != null && recordEntityList.size() > 0){
            for (AccountRecordEntity accountRecordEntity : recordEntityList) {
                totalIncome = totalIncome.add(accountRecordEntity.getAmount());
            }
        }
        map.put("downIncome",totalIncome); //店铺已收款
        //未完成的收款
        recordEntityList = accountRecordDao.findByToUserId(userId, type, bus, AccountRecordStatusEnum.WAIT.getIndex());
        if (recordEntityList != null && recordEntityList.size() > 0){
            for (AccountRecordEntity accountRecordEntity : recordEntityList) {
                totalIncome = totalIncome.add(accountRecordEntity.getAmount());
            }
        }
        map.put("totalIncome",totalIncome); // 店铺实际收款
        return map;
    }

    /** que
     * 待收款
     * @param userId
     * @return
     */
    @Override
    public BigDecimal waitIncome(Long userId) {
        BigDecimal total = new BigDecimal(0);
        List<AccountRecordEntity> accountRecordEntities =  accountRecordDao.waitIncome(userId,AccountRecordStatusEnum.WAIT.getIndex(),AccountRecordBusEnum.PAYMENT.getIndex(),AccountRecordBusEnum.SHARE_REWARD.getIndex(),AccountRecordBusEnum.AREA_REWARD.getIndex(),AccountRecordBusEnum.SHOP.getIndex(),AccountRecordBusEnum.SHOP_MANAGER_REWARD.getIndex());
        if (accountRecordEntities != null && accountRecordEntities.size() > 0){
            for (AccountRecordEntity accountRecordEntity : accountRecordEntities) {
                //只计算代金券 优惠券 换购券 清货券金额
                if (accountRecordEntity.getType() == AccountTypeEnum.CASH.getIndex()
                        ||  accountRecordEntity.getType() == AccountTypeEnum.VOUCHER.getIndex()
                        || accountRecordEntity.getType() == AccountTypeEnum.COUPON.getIndex()
                        || accountRecordEntity.getType() == AccountTypeEnum.CLEAR.getIndex()
                        || accountRecordEntity.getType() == AccountTypeEnum.WELFARE.getIndex()){
                    total = total.add(accountRecordEntity.getAmount());
                }
            }
        }
        return total;
    }
}
